//
//  XDNextStepViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-17.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDNextStepViewController.h"
#import "ActivityInfoModel.h"
#import "OperationInfoModel.h"
#import "TargetActivityInfoModel.h"
#import "XDPersonChooseViewController.h"
#import "TransactorModel.h"
#import "Handle_DealWaitHandle_Task.h"

#define HandlerPerson_STORE_FlAG @"HandlerPerson_STORE_FlAG"
#define HandlerSYDPerson_STORE_FlAG @"HandlerSYDPerson_STORE_FlAG"

@interface XDNextStepViewController ()
{
    ActivityInfoModel *am;
    OperationInfoModel *om;
    NSMutableArray *jjcdData;
    UIPickerView *pvJjcd;
    int pushflag,alertflag;
    NSMutableString *handlerPersonGuids,*sydPersonGuids;
    EpointSingleChooseAlertView *jjcdAlert,*filterTransactorAlter;
    TargetActivityInfoModel *t;
    NSString *submitTaskGuid;
}
@end

@implementation XDNextStepViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        jjcdData = [[NSMutableArray alloc]initWithObjects:@"一般",@"重要",@"紧急", nil];
        
        pushflag = 0;
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud removeObjectForKey:HandlerPerson_STORE_FlAG];
        [ud removeObjectForKey:HandlerSYDPerson_STORE_FlAG];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    handlerPersonGuids = [[NSMutableString alloc]initWithString:@""];
    sydPersonGuids = [[NSMutableString alloc]initWithString:@""];
    
    [UITool setGlossyButtonWithRed:68.0f green:151.0f blue:200.0f :[self.view viewWithTag:1001]];
    pvJjcd = [[UIPickerView alloc]initWithFrame:CGRectMake(0,700, 320, 260)];
    pvJjcd.showsSelectionIndicator = YES;
    [pvJjcd setDelegate:self];
    [pvJjcd setDataSource:self];
    [self.view addSubview:pvJjcd];
    
    self.title = @"事项处理";
    am = [[intentParams objectForKey:@"activityInfoModel"]retain];
    om = [[intentParams objectForKey:@"OperationInfoModel"]retain];
    
    if ([@"shift" isEqualToString:om.OperationType]) {
        self.ActivityName.text = [NSString stringWithFormat:@"【送%@】处理人：",om.OperationName];
        [uvSyd setHidden:YES];
        [self.sv setContentSize:CGSizeMake(320, 520)];
        self.lbDqbz.text = am.ActivityDispName;
        self.lbDqcz.text = om.OperationName;
    }else if([@"back" isEqualToString:om.OperationType]){
        [self.uvHandlePerson setHidden:YES];
        [self.uvOpinion setFrame:CGRectMake(0, 76, self.uvOpinion.frame.size.width, self.uvOpinion.frame.size.height)];
        [uvSyd setHidden:YES];
    }else{
        if([om.IsSongPY isEqualToString:@"1"]&&[om.PYNeedUsers isEqualToString:@"0"])
        {
            [self.uvHandlePerson setHidden:YES];
            [self.uvOpinion setFrame:CGRectMake(0, 76, self.uvOpinion.frame.size.width, self.uvOpinion.frame.size.height)];
            [uvSyd setHidden:YES];
        }
        else if([om.IsSongPY isEqualToString:@"1"]&&[om.PYNeedUsers isEqualToString:@"1"])
        {
            self.ActivityName.text = [NSString stringWithFormat:@"【%@】处理人：",@"送批办"];
        }
        else
        {
            [uvSyd setHidden:YES];
        }
        
        NSString *fz = [intentParams objectForKey:@"fz10"];
        int item = 0;
        if (fz==nil)
        {
            fz = [intentParams objectForKey:@"fz30"];
        }
        
        if (fz==nil) {
            fz = @"-1";
        }
        
        item = fz.intValue;
        
        [self.sv setContentSize:CGSizeMake(320, 520)];
        self.lbDqbz.text = am.ActivityDispName;
        self.lbDqcz.text = om.OperationName;
        
        
        
        
        if (item>=0) {
            
            t = [om.TargetActivityList objectAtIndex:item];
            self.ActivityName.text = [NSString stringWithFormat:@"【%@】处理人：",t.ActivityName];
            NSLog(@"活动名称：%@",t.ActivityName);
            if (t.DefaultTransactorList.count>0){
                NSMutableString *defaultguids = [[NSMutableString alloc]initWithString:@""];
                NSMutableString *defaultnames = [[NSMutableString alloc]initWithString:@""];
                
                for (int i=0; i<t.DefaultTransactorList.count; i++) {
                    TransactorModel *tm = [t.DefaultTransactorList objectAtIndex:i];
                    [defaultguids appendString:tm.UserGuid];
                    [defaultnames appendString:tm.DisplayName];
                    if (i!=t.DefaultTransactorList.count-1) {
                        [defaultguids appendString:@";"];
                        [defaultnames appendString:@";"];
                    }
                    
                }
                handlerPersonGuids = [defaultguids retain];
                self.lbHandlerPerson.text = defaultnames;
                
                NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
                NSString *names = defaultnames;
                NSString *guids = handlerPersonGuids;
                
                if (names!=nil&&guids!=nil) {
                    pushflag = 1;
                    NSArray *namearr = [defaultnames componentsSeparatedByString:@";"];
                    NSArray *guidarr = [handlerPersonGuids componentsSeparatedByString:@";"];
                    
                    NSMutableArray *passedPerson = [[NSMutableArray alloc]init];
                    for (int i=0; i<namearr.count; i++) {
                        NSMutableDictionary *sp = [[NSMutableDictionary alloc]init];
                        [sp setObject:[namearr objectAtIndex:i] forKey:@"displayname"];
                        [sp setObject:[guidarr objectAtIndex:i] forKey:@"userguid"];
                        [passedPerson addObject:sp];
                        [sp release];
                    }
                    [ud setObject:passedPerson forKey:HandlerPerson_STORE_FlAG];
                    [passedPerson release];
                }
            }
            [self.uvOpinion setFrame:CGRectMake(0, 175, self.uvOpinion.frame.size.width, self.uvOpinion.frame.size.height)];
        }
        
        if ([t.Is_TargetTransactor_Editable isEqualToString:@"20"]) {
            [self.uvHandlePerson setHidden:YES];
            [self.uvOpinion setFrame:CGRectMake(0, 76, self.uvOpinion.frame.size.width, self.uvOpinion.frame.size.height)];
        }
        
        if (t.TransactorFilterList.count>0) {
            NSLog(@"过滤人：%i",t.TransactorFilterList.count);
        }
    }
       
}
- (IBAction)submitAction:(id)sender {
    
    
    if (!self.uvHandlePerson.isHidden&&self.lbHandlerPerson.text.length==0) {
        
        [UITool toast:@"请选择下一步处理人！"];
        return;
    }
    
    NSMutableString *sb = [[NSMutableString alloc]initWithString:@""];
    if(![om.IsSongPY isEqualToString:@"1"]){
        [sb appendString:@"<TargetActivityList>"];
        [sb appendString:@"<TargetActivityInfo>"];
        [sb appendString:@"<ActivityGuid>"];
        if (t!=nil) {
            [sb appendString:t.ActivityGuid];
        }
        [sb appendString:@"</ActivityGuid>"];
        [sb appendString:@"<HandlePersonList>"];
        
        NSArray *cdarr = [handlerPersonGuids componentsSeparatedByString:@";"];
        for (int i=0; i<cdarr.count; i++) {
            [sb appendString:@"<HandlePerson>"];
            [sb appendString:[cdarr objectAtIndex:i]];
            [sb appendString:@"</HandlePerson>"];
        }
        [sb appendString:@"</HandlePersonList>"];
        [sb appendString:@"</TargetActivityInfo>"];
        [sb appendString:@"</TargetActivityList>"];
    }
    
    
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:[intentParams objectForKey:@"MessageItemGuid"] forKey:@"MessageItemGuid"];
    [params setObject:sb forKey:@"TargetActivityList"];
    [params setObject:self.tvOpinion.text forKey:@"Opinion"];
    [params setObject:om.OperationGuid forKey:@"OperationGuid"];
    [params setObject:@"" forKey:@"PDFPath"];
    
    [params setObject:handlerPersonGuids forKey:@"PiBanUserGuidLst"];
    [params setObject:sydPersonGuids forKey:@"YueDuUserGuidLst"];
    
    
    if ([self.cbDxtx isOn]) {
        [params setObject:@"1" forKey:@"SendSms"];
    }
    else
    {
        [params setObject:@"0" forKey:@"SendSms"];
    }
    
    if ([self.btJjcd.titleLabel.text isEqualToString:[jjcdData objectAtIndex:0]]) {
        [params setObject:@"1" forKey:@"SendSms_JJCD"];
    }
    else if([self.btJjcd.titleLabel.text isEqualToString:[jjcdData objectAtIndex:1]]) {
        [params setObject:@"2" forKey:@"SendSms_JJCD"];
    }
    else if([self.btJjcd.titleLabel.text isEqualToString:[jjcdData objectAtIndex:2]]) {
        [params setObject:@"3" forKey:@"SendSms_JJCD"];
    }
    
    [params setObject:@"" forKey:@"PDFPath"];
    [self showMBPHUDWithMessage:@"正在提交..."];
    submitTaskGuid = [[[Handle_DealWaitHandle_Task alloc]initWithContext:self andParams:params]startTask];
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([threadGuid isEqualToString:submitTaskGuid]) {
        NSString *bs = (NSString *)obj;
        if ([self validateXML:bs]) {
            [UITool toast:@"处理成功"];
            [self.navigationController popToViewController:[intentParams objectForKey:@"listContext"] animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_lbDqbz release];
    [_lbDqcz release];
    [_ActivityName release];
    [_sv release];
    [_tvOpinion release];
    [_btJjcd release];
    
    [_lbHandlerPerson release];
    [_uvHandlePerson release];
    [_uvOpinion release];
    [_cbDxtx release];
    [uvSyd release];
    [tfSydClr release];
    [btSydClr release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setLbDqbz:nil];
    [self setLbDqcz:nil];
    [self setActivityName:nil];
    [self setSv:nil];
    [self setTvOpinion:nil];
    [self setBtJjcd:nil];
    
    [self setLbHandlerPerson:nil];
    [self setUvHandlePerson:nil];
    [self setUvOpinion:nil];
    [self setCbDxtx:nil];
    [uvSyd release];
    uvSyd = nil;
    [tfSydClr release];
    tfSydClr = nil;
    [btSydClr release];
    btSydClr = nil;
    [super viewDidUnload];
}

#pragma textview delegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [self.sv setContentOffset:CGPointMake(0, 100) animated:YES];
        return NO;
    }
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [self.sv setContentOffset:CGPointMake(0, 100) animated:YES];
    return YES;
}

#pragma pickerview

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 3;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [jjcdData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.btJjcd.titleLabel.text = [jjcdData objectAtIndex:row];
}


- (IBAction)addHandlerPerson:(id)sender {
    
    if (t.TransactorFilterList.count>0) {
        NSLog(@"请选择默认处理人");
        NSMutableArray *transactorFilterArr = [[NSMutableArray alloc]init];
        for (int i=0; i<t.TransactorFilterList.count; i++) {
            TransactorModel *ft = [t.TransactorFilterList objectAtIndex:i];
            [transactorFilterArr addObject:ft.DisplayName];
        }
        filterTransactorAlter = [[EpointSingleChooseAlertView alloc]initWithArray:transactorFilterArr title:@"选择处理人"];
        [filterTransactorAlter setDelegate:self];
        [self.view addSubview:filterTransactorAlter];
        alertflag = 2;
        return;
    }
    
    pushflag = 1;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if ([ud objectForKey:HandlerPerson_STORE_FlAG]!=nil) {
        [params setObject:[ud objectForKey:HandlerPerson_STORE_FlAG] forKey:@"withPerson"];
    }
    [params setObject:HandlerPerson_STORE_FlAG forKey:@"whichControl"];
    XDPersonChooseViewController *vc = [[XDPersonChooseViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)chooseJjcd:(id)sender {
    jjcdAlert = [[EpointSingleChooseAlertView alloc]initWithArray:jjcdData title:@"请选择"];
    jjcdAlert.delegate = self;
    [self.view addSubview:jjcdAlert];
    alertflag = 1;
}

- (IBAction)chooseSydPerson:(id)sender {
    pushflag = 2;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if ([ud objectForKey:HandlerSYDPerson_STORE_FlAG]!=nil) {
        [params setObject:[ud objectForKey:HandlerSYDPerson_STORE_FlAG] forKey:@"withPerson"];
    }
    [params setObject:HandlerSYDPerson_STORE_FlAG forKey:@"whichControl"];
    XDPersonChooseViewController *vc = [[XDPersonChooseViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)epointSingleChooseViewEndSelected:(EpointSingleChooseAlertView *)epointSingleChooseAlertView
{
    if (alertflag==1) {
        [self.btJjcd setTitle:epointSingleChooseAlertView.selectedItem forState:UIControlStateNormal];
    }
    else if(alertflag==2)
    {
        self.lbHandlerPerson.text = [[t.TransactorFilterList objectAtIndex:epointSingleChooseAlertView.selectedIndex]DisplayName];
        handlerPersonGuids = (NSMutableString *)[[t.TransactorFilterList objectAtIndex:epointSingleChooseAlertView.selectedIndex]UserGuid];
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    if (pushflag==1) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSMutableArray *choosedPerson = [ud objectForKey:HandlerPerson_STORE_FlAG];
        NSMutableString *names = [[NSMutableString alloc]initWithString:@""];
        handlerPersonGuids = [[NSMutableString alloc]initWithString:@""];
        for (int i=0; i<choosedPerson.count; i++) {
            NSMutableDictionary *dic = [choosedPerson objectAtIndex:i];
            [names appendString:[dic objectForKey:@"displayname"]];
            [names appendString:@";"];
            
            [handlerPersonGuids appendString:[dic objectForKey:@"userguid"]];
            [handlerPersonGuids appendString:@";"];
        }
        self.lbHandlerPerson.text = names;
        [names release];
    }
    else if(pushflag==2)
    {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSMutableArray *choosedPerson = [ud objectForKey:HandlerSYDPerson_STORE_FlAG];
        NSMutableString *names = [[NSMutableString alloc]initWithString:@""];
        sydPersonGuids = [[NSMutableString alloc]initWithString:@""];
        for (int i=0; i<choosedPerson.count; i++) {
            NSMutableDictionary *dic = [choosedPerson objectAtIndex:i];
            [names appendString:[dic objectForKey:@"displayname"]];
            [names appendString:@";"];
            
            [sydPersonGuids appendString:[dic objectForKey:@"userguid"]];
            [sydPersonGuids appendString:@";"];
        }
        tfSydClr.text = names;
        
        [names release];
    }
    
}

@end


